package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Employee;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.dao.EmployeeDao;
import com.qf.dao.MenuMapper;
import com.qf.dao.RmMapper;
import com.qf.dao.RoleMapper;
import com.qf.service.EmployeeService;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import com.qf.utils.constant.PageUils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 21/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findRoleList(Integer did) {
        List<Role> roleList = roleMapper.findRoleList(did);
        return roleList;
    }

    @Override
    public List<Role> findByDid(Integer roleid) {
        List<Role> roleList = roleMapper.findByDid(roleid);
        return roleList;
    }

    @Override
    public Role findByRoleId(Integer roleid) {
        Role role = roleMapper.findRoleId(roleid);
        return role;
    }

    @Override
    public PageInfo<Role> showPage(Integer index, String rname, String dept, Integer state) {
        PageHelper.startPage(index, PageUils.PAGESIZE.getSize());
        HashMap map = new HashMap();
        map.put("rname", rname);
        map.put("deptname", dept);
        map.put("rstate", state);
        System.out.println("service" + state);
        List<Role> roleList = roleMapper.showPage(map);
        PageInfo<Role> pageInfo = new PageInfo(roleList);
        return pageInfo;

    }

    @Autowired
    private RmMapper rmMapper;

    @Transactional
    @Override
    public Integer addRole(Role role) {
        Integer i = roleMapper.add(role);
        HashMap map = new HashMap();
        Integer roleid = role.getRoleid();
        map.put("roleid", roleid);
        Integer[] menuids = role.getMenuids();
        map.put("menuids", menuids);
        Integer integer = rmMapper.addMenusInMid(map);
        return integer;
    }

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @Override
    public Role updateShowBack(Integer roleid) {
        Role role = roleService.findByRoleId(roleid);
        System.out.println(role);
        List<Menu> menus = menuService.findByRole(role.getRoleid());//找到该角色的所有的菜单集合

        ArrayList firstMenu = new ArrayList();//保存一级菜单
        for (Menu menu : menus) {
            if (menu.getUpmenuid() == 0) { //一级菜单
                ArrayList secondList = new ArrayList();//保存二级菜单
                for (Menu second : menus) {
                    if (menu.getMenuid() == second.getUpmenuid()) {//如果一级菜单的id(menuid)==二级菜单的父id(upmenuid)
                        secondList.add(second);
                    }
                }
                //将二级菜单的集合封装到一级菜单中
                menu.setSecondList(secondList);
                //将一级菜单在添加到集合中
                firstMenu.add(menu);
            }
        }

        //3.将菜单赋值给角色，角色再赋值给用户
        role.setMenuList(firstMenu);
        return role;
    }

    @Transactional
    @Override
    public Integer update(Role role) {
        Integer a = roleMapper.update(role);
        Integer b = rmMapper.delete(role.getRoleid());
        HashMap map = new HashMap();
        map.put("roleid", role.getRoleid());
        map.put("menuids", role.getMenuids());
        Integer c = rmMapper.updateMenu(map);

        return c;
    }
@Autowired
private EmployeeDao employeeDao;
    @Transactional
    @Override
    public Integer deleterole(Integer roleid) {
       List<Employee> employees= employeeDao.findByRoleId(roleid);
        System.out.println("======================"+employees);
        System.out.println(employees.size()+">>>>>>>");
       if (employees.size()!=0){
           return -1;
       }else{
           Integer a = rmMapper.delete(roleid);
           Integer b=roleMapper.deleteByRoleId(roleid);
           return b;
       }
    }

    @Transactional
    @Override
    public Integer deleteByRoleIds(Integer[] roleids) {
        //1.查询用户表中是否有关联的角色id
        System.out.println("======================="+roleids);
        List<Employee> employees=employeeDao.findByRoleIds(roleids);
        if (employees.size()!=0){
            return -1;
        }else{
            //2.清除中间表的相关roleid的信息
           Integer b= rmMapper.deleteByRoleIds(roleids);
           //3.清除角色表中信息
            Integer c=roleMapper.deleteByRoleIds(roleids);
            return c;
        }
    }

    @Override
    public Integer deleteByRoleId(Integer roleid) {
        return null;
    }
}
